package com.quyunshuo.module.home.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class MyLineProgressView(
    context: Context,
    attrs: AttributeSet? = null,
) : View(context, attrs) {
    // 画笔
    private val bgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
    }

    private val progressPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeCap = Paint.Cap.ROUND // 进度条两端为圆角
    }

    // 默认颜色值
    private var defaultBackgroundColor = Color.LTGRAY
    private var defaultProgressColor = Color.BLUE

    // 进度值 (0-100)
    var progress: Float = 10f
        set(value) {
            field = value.coerceIn(0f, 100f) // 限制在0-100范围
            invalidate() // 更新UI
        }

    // 线条高度
    private var lineHeight: Float = 15f

    init {
        // 设置画笔
        bgPaint.strokeWidth = lineHeight
        bgPaint.color = defaultBackgroundColor

        progressPaint.strokeWidth = lineHeight
        progressPaint.color = defaultProgressColor
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // 处理高度测量：高度应该至少等于线条高度
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        var heightSize = MeasureSpec.getSize(heightMeasureSpec)

        // 如果高度测量模式是AT_MOST或UNSPECIFIED（wrap_content，也就是没有指定高度的时候），设置最小高度
        if (heightMode != MeasureSpec.EXACTLY) {
            val minHeight = (lineHeight + paddingTop + paddingBottom).toInt()
            heightSize = minHeight.coerceAtLeast(heightSize)
        }

        // 宽度使用父布局传递的值
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)

        setMeasuredDimension(widthSize, heightSize)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        // 线条位置会在onDraw中根据高度计算
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 计算线条Y坐标位置（垂直居中）
        val centerY = height / 2f

        // 可用宽度（减去两端的padding）
        val usableWidth = width - paddingStart - paddingEnd

        // 1. 绘制背景线
        canvas.drawLine(
            paddingStart.toFloat(), centerY,
            paddingStart + usableWidth.toFloat(), centerY,
            bgPaint
        )

        // 2. 绘制进度线（如果进度大于0）
        if (progress > 0) {
            // 计算进度线长度
            val progressWidth = usableWidth * progress / 100f

            // 绘制进度线（起始点相同，终点随进度变化）
            canvas.drawLine(
                paddingStart.toFloat(), centerY,
                paddingStart + progressWidth, centerY,
                progressPaint
            )
        }
    }

    // 设置线条高度（以像素为单位）
    fun setLineHeight(height: Float) {
        lineHeight = height
        bgPaint.strokeWidth = height
        progressPaint.strokeWidth = height
        requestLayout() // 需要重新测量布局
        invalidate()
    }

    // 设置背景颜色
//    fun setBackgroundColor(color: Int) {
//        bgPaint.color = color
//        invalidate()
//    }

    // 设置进度颜色
    fun setProgressColor(color: Int) {
        progressPaint.color = color
        invalidate()
    }

}